# coding: utf-8
#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can rig the roll foot
#Note: didn't run only, you should have a leg rig file.if only a foot, ik is bad.
#Author: rosa.w
#FN: rigfoot(Direction)
#Date: 2012/011/16_v1.0
import maya.cmds as mc
import RosaCommands as rc
import rosa_CreateJntForSpacePoint as rcj
import rosa_addShape as ra

reload(rc)

def rigfoot(dir):

        #'''At the first,you should have a foot space point file , it is used to create joint '''
        #create a final hierarchy with locator,at the last,driven this loc's rotate.


        if dir != 'Left' and dir != 'Right':
                mc.error('---->>>> please wrist a current direction . <<<<----')

        if  mc.objExists(dir+'_Foot_final_Hierarchy'):
                mc.error('------->>>> This object has been on the scene there , please delete it or open a new sence ! <<<<--------')

        #get worldspace from the joint.
        ballpos = rc.getSpace( dir+'_foot02_Drv')
        toepos = rc.getSpace( dir+'_foot03_Drv')
        heelpos = rc.getSpace( dir+'_Heel_Drv')
        outsidepos = rc.getSpace( dir+'_Outside_Drv' )
        insidepos = rc.getSpace( dir+'_Inside_Drv')
        footpos = rc.getSpace( dir+'_foot01_Drv')


        ball_loc = mc.spaceLocator(p=( 0 , 0 , 0 ) , n=dir+'_ball_loc' )[0]
        toe_loc = mc.spaceLocator(p=(  0 , 0 , 0  ) , n=dir+'_toe_loc' )[0]
        heel_loc = mc.spaceLocator(p=( 0 , 0 , 0 ) , n=dir+'_heel_loc' )[0]
        outside_loc = mc.spaceLocator(p=(  0 , 0 , 0  ) , n=dir+'_outside_loc')[0]
        inside_loc = mc.spaceLocator(p=(  0 , 0 , 0  ) , n=dir+'_inside_loc' )[0]
        foot_loc = mc.spaceLocator(p=(  0 , 0 , 0  ) , n=dir+'_foot_loc' )[0]

        pointJntlist  = [  dir+'_foot02_Drv' , dir+'_foot03_Drv' , dir+'_Heel_Drv' , dir+'_Outside_Drv' , dir+'_Inside_Drv' , dir+'_foot01_Drv']


        loclist = [  ball_loc , toe_loc , heel_loc , outside_loc , inside_loc , foot_loc  ]
        loclistpos = [  ballpos , toepos , heelpos , outsidepos , insidepos , footpos  ]



        for i in range ( len(loclist) ):
                mc.xform( loclist[i] , t= (loclistpos[i][0] ,  loclistpos[i][1]  , loclistpos[i][2] )  , ws=True )
                mc.orientConstraint(pointJntlist[i] , loclist[i],mo=False)
                mc.select(loclist[i])
                mc.delete(cn=True)

        for i in range( len(loclist)-1 ):
                mc.parent( loclist[i] , loclist[i+1] )
                mc.select(cl=True)

        for i in range( len(loclist) ):
                mc.select(loclist[i])
                rc.makeObjZero('group' , 'pos' , 'On')

        #add a loc #-_-!
        toeroll_loc = mc.spaceLocator(p = (0 , 0 , 0) , n=dir+'+toeRoll_loc' )[0]
        mc.xform(toeroll_loc , t=(ballpos[0] ,  ballpos[1]  , ballpos[2] ) , ws=True)
        mc.orientConstraint( pointJntlist[0] , toeroll_loc , mo=False)
        mc.select(toeroll_loc)
        mc.delete(cn=True)
        rc.makeObjZero('group' , 'pos' , 'On')
        mc.parent( toeroll_loc+'_pos' , toe_loc)



        #copy and rename joint.
        drvJnt = rc.copyandRenameJoint( dir + '_foot01_Drv' , dir+'_foot' , 'drvJnt')
        ikJnt = rc.copyandRenameJoint( dir + '_foot01_Drv' , dir+'_foot' , 'ikJnt')
        fkJnt = rc.copyandRenameJoint( dir + '_foot01_Drv' , dir+'_foot' , 'fk_ctrl')
        mc.delete(dir + '_foot01_Drv')




#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#





        #IK SYSYTEM RUNNING.......

        #MLGBD.......... create ikhandle and return ikhandle  ikeffector .
        ik01sys = rc.createikhandle( ikJnt[0] , ikJnt[1] , 'ikhandle' , dir+'_ball' )
        ik02sys = rc.createikhandle( ikJnt[1] , ikJnt[2] , 'ikhandle' , dir+'_toe' )

        #create a bridge group.
        footCmd_bridge = mc.createNode( 'transform' , n= dir+'_footCmd_Bridge')
        rc.addNewAttr( footCmd_bridge , 0 ,-100 , 100 , '___footCmd' , 'toelift' , 'toestraight' , 'roll' , 'side' , 'ballroll' , 'heelspin' , 'toespin' , 'toeroll' , 'toewiggle' )
        rc.addNewAttr( footCmd_bridge , 0 ,0 , 1 , 'ikfk')
        rc.lockhideAttr(footCmd_bridge , 1 ,'___footCmd')
        rc.lockhideAttr(footCmd_bridge , 0 ,'translateX' , 'translateY' , 'translateZ' , 'rotateX' , 'rotateY' , 'rotateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility')

        #give toelift and toestraight attribute a defaulte value for roll system.
        mc.setAttr(footCmd_bridge+'.toelift' , 45)
        mc.setAttr(footCmd_bridge+'.toestraight' , 90)


        #rig rotate poivt locator.
        # use a big ifelse , because i think it is clear ,

        #first rig roll system.
        #helpNode.    these two groups will pass the value to toeloc and ballloc,because this two locs have been drivened.
        toeroll_bridge = mc.createNode('transform' , n = dir+'toeRoll_help_grp')
        ballroll_bridge = mc.createNode('transform' , n = dir+'ballRoll_help_grp')
        roll_revesebridge = mc.createNode('multiplyDivide' , n = dir+'rollvalueReversNode')

        #create node
        setRange01node = mc.createNode('setRange' , n = dir+'_foot_roll01_setRangeNode')
        setRange02node = mc.createNode('setRange' , n = dir+'_foot_roll02_setRangeNode')
        setRange03node = mc.createNode('setRange' , n = dir+'_foot_roll03_setRangeNode')

        md01node = mc.createNode('multiplyDivide' , n = dir+'_foot_roll01_mdNode')

        pa01node =mc.createNode('plusMinusAverage' , n = dir+'_foot_roll01_paNode')
        pa02node =mc.createNode('plusMinusAverage' , n = dir+'_foot_roll02_paNode')
        pa03node =mc.createNode('plusMinusAverage' , n = dir+'_foot_roll03_paNode')
        pa04node =mc.createNode('plusMinusAverage' , n = dir+'_foot_roll04_paNode')


        #connect attribute
        mc.setAttr(setRange01node+'.maxX' , 1)
        mc.connectAttr(footCmd_bridge+'.toelift' , setRange01node+'.oldMaxX')
        mc.connectAttr(footCmd_bridge+'.roll' , setRange01node+'.valueX')

        mc.setAttr(setRange02node+'.maxX' , 1)
        mc.connectAttr(footCmd_bridge+'.toelift' , setRange02node+'.oldMinX')
        mc.connectAttr(footCmd_bridge+'.toestraight' , setRange02node+'.oldMaxX')
        mc.connectAttr(footCmd_bridge+'.roll' , setRange02node+'.valueX')

        mc.setAttr(pa01node+'.operation' , 2)
        mc.connectAttr(setRange01node+'.outValueX' , pa01node+'.input1D[0]')
        mc.connectAttr(setRange02node+'.outValueX' , pa01node+'.input1D[1]')
        mc.connectAttr(pa01node+'.output1D' , md01node+'.input2X')
        mc.connectAttr(footCmd_bridge+'.roll'  , md01node+'.input1X')

        mc.setAttr(setRange03node+'.maxX' , 1)
        mc.connectAttr(footCmd_bridge+'.toelift' , setRange03node+'.oldMinX')
        mc.connectAttr(footCmd_bridge+'.toestraight' , setRange03node+'.oldMaxX')
        mc.connectAttr(footCmd_bridge+'.roll' , setRange03node+'.valueX')
        mc.connectAttr(setRange03node+'.outValueX' , md01node+'.input2Y')
        mc.connectAttr(footCmd_bridge+'.roll' , md01node+'.input1Y')

        mc.setAttr(roll_revesebridge+'.input2X' , -1)
        mc.setAttr(roll_revesebridge+'.input2Y' , -1)
        mc.connectAttr(md01node+'.outputX' , roll_revesebridge+'.input1X')
        mc.connectAttr(md01node+'.outputY' , roll_revesebridge+'.input1Y')
        #Secound rig other foot commands.

        if dir == 'Left':

                #Side
                rc.setDrv(dir+'_outside'   ,  outside_loc +'.rotateZ' ,0   ,  footCmd_bridge +'.side' , 0)
                rc.setDrv(dir+'_outside'   ,  outside_loc +'.rotateZ' ,-50   ,  footCmd_bridge+'.side' , 50)
                mc.selectKey(outside_loc+'.rotateZ' )
                mc.setInfinity(poi='cycleRelative')

                rc.setDrv(dir+'_inside'   ,  inside_loc+'.rotateZ' ,0   ,  footCmd_bridge+'.side' , 0)
                rc.setDrv(dir+'_inside'   ,  inside_loc+'.rotateZ' ,50   ,  footCmd_bridge+'.side' , -50)
                mc.selectKey(inside_loc+'.rotateZ' )
                mc.setInfinity(pri='cycleRelative')

                #Heelspin
                rc.setDrv(dir+'_heel'   ,  heel_loc+'.rotateY' ,0   ,  footCmd_bridge+'.heelspin' , 0)
                rc.setDrv(dir+'_heel'   ,  heel_loc+'.rotateY' ,30   ,  footCmd_bridge+'.heelspin' , 20)
                mc.selectKey(heel_loc+'.rotateY' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Toespin
                rc.setDrv(dir+'_toeSpin'   ,  toe_loc+'.rotateY' ,0   ,  footCmd_bridge+'.toespin' , 0)
                rc.setDrv(dir+'_toeSpin'   ,  toe_loc+'.rotateY' ,30   ,  footCmd_bridge+'.toespin' , 20)
                mc.selectKey(toe_loc+'.rotateY' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Toeroll (includ bridge group)
                rc.setDrv(dir+'_toeroll'   ,  toeroll_bridge+'.rotateX' ,0   ,  footCmd_bridge+'.toeroll' , 0)
                rc.setDrv(dir+'_toeroll'   ,  toeroll_bridge+'.rotateX' ,-30   ,  footCmd_bridge+'.toeroll' , 20)
                mc.selectKey(toeroll_bridge+'.rotateX' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Ballroll (includ bridge group)
                rc.setDrv(dir+'_ballroll'   ,  ballroll_bridge+'.rotateX' ,0   ,  footCmd_bridge+'.ballroll' , 0)
                rc.setDrv(dir+'_ballroll'   ,  ballroll_bridge+'.rotateX' ,-30   ,  footCmd_bridge+'.ballroll' , 20)
                mc.selectKey(ballroll_bridge+'.rotateX' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Toewiggle
                rc.setDrv(dir+'_toewiggle'   ,  toeroll_loc+'.rotateZ' ,0   ,  footCmd_bridge+'.toewiggle' , 0)
                rc.setDrv(dir+'_toewiggle'   ,  toeroll_loc+'.rotateZ' ,30   ,  footCmd_bridge+'.toewiggle' , 30)
                mc.selectKey(toeroll_loc+'.rotateZ' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #heel(for roll)
                rc.setDrv(dir+'_heelroll'   ,  heel_loc+'.rotateX' ,0   ,  footCmd_bridge+'.roll' , 0)
                rc.setDrv(dir+'_heelroll'   ,  heel_loc+'.rotateX' ,-30   ,  footCmd_bridge+'.roll' , -20)
                mc.selectKey(heel_loc+'.rotateX' )
                mc.setInfinity( pri='cycleRelative'  )

        elif dir == 'Right':

                #Side
                rc.setDrv(dir+'_outside'   ,  outside_loc+'.rotateZ' ,0   ,  footCmd_bridge+'.side' , 0)
                rc.setDrv(dir+'_outside'   ,  outside_loc+'.rotateZ' ,-50   ,  footCmd_bridge+'.side' , 50)
                mc.selectKey(outside_loc+'.rotateZ' )
                mc.setInfinity(poi='cycleRelative')

                rc.setDrv(dir+'_inside'   ,  inside_loc+'.rotateZ' ,0   ,  footCmd_bridge+'.side' , 0)
                rc.setDrv(dir+'_inside'   ,  inside_loc+'.rotateZ' ,50   ,  footCmd_bridge+'.side' , -50)
                mc.selectKey(inside_loc+'.rotateZ' )
                mc.setInfinity(pri='cycleRelative')

                #Heelspin
                rc.setDrv(dir+'_heel'   ,  heel_loc+'.rotateY' ,0   ,  footCmd_bridge+'.heelspin' , 0)
                rc.setDrv(dir+'_heel'   ,  heel_loc+'.rotateY' ,30   ,  footCmd_bridge+'.heelspin' , 20)
                mc.selectKey(heel_loc+'.rotateY' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Toespin
                rc.setDrv(dir+'_toeSpin'   ,  toe_loc+'.rotateY' ,0   ,  footCmd_bridge+'.toespin' , 0)
                rc.setDrv(dir+'_toeSpin'   ,  toe_loc+'.rotateY' ,30   ,  footCmd_bridge+'.toespin' , 20)
                mc.selectKey(toe_loc+'.rotateY' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Toeroll
                rc.setDrv(dir+'_toeroll'   ,  toeroll_bridge+'.rotateX' ,0   ,  footCmd_bridge+'.toeroll' , 0)
                rc.setDrv(dir+'_toeroll'   ,  toeroll_bridge+'.rotateX' ,-30   ,  footCmd_bridge+'.toeroll' , 20)
                mc.selectKey(toeroll_bridge+'.rotateX' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                 #Ballroll
                rc.setDrv(dir+'_ballroll'   ,  ballroll_bridge+'.rotateX' ,0   ,  footCmd_bridge+'.ballroll' , 0)
                rc.setDrv(dir+'_ballroll'   ,  ballroll_bridge+'.rotateX' ,-30   ,  footCmd_bridge+'.ballroll' , 20)
                mc.selectKey(ballroll_bridge+'.rotateX' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #Toewiggle
                rc.setDrv(dir+'_toewiggle'   ,  toeroll_loc+'.rotateZ' ,0   ,  footCmd_bridge+'.toewiggle' , 0)
                rc.setDrv(dir+'_toewiggle'   ,  toeroll_loc+'.rotateZ' ,30   ,  footCmd_bridge+'.toewiggle' , 30)
                mc.selectKey(toeroll_loc+'.rotateZ' )
                mc.setInfinity( pri='cycleRelative' , poi='cycleRelative' )

                #heel(for roll)
                rc.setDrv(dir+'_heelroll'   ,  heel_loc+'.rotateX' ,0   ,  footCmd_bridge+'.roll' , 0)
                rc.setDrv(dir+'_heelroll'   ,  heel_loc+'.rotateX' ,-30   ,  footCmd_bridge+'.roll' , -20)
                mc.selectKey(heel_loc+'.rotateX' )
                mc.setInfinity( pri='cycleRelative'  )

        mc.connectAttr(toeroll_bridge+'.rotateX' , pa02node+'.input1D[0]')
        mc.connectAttr(roll_revesebridge+'.outputY' , pa02node+'.input1D[1]')
        mc.connectAttr(pa02node+'.output1D' , toe_loc+'.rotateZ')

        mc.connectAttr(ballroll_bridge+'.rotateX' , pa03node+'.input1D[0]')
        mc.connectAttr(roll_revesebridge+'.outputX' , pa03node+'.input1D[1]')
        mc.connectAttr(pa03node+'.output1D' ,ball_loc+'.rotateZ')


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#


        #ikfk change system.

        switchCmdGrp = rc.drvCtr_swich(dir+'_foot' , ikJnt[0] , fkJnt[0] , drvJnt[0] , 0)
        rc.lockhideAttr(switchCmdGrp , 0 , 'translateX' , 'translateY' , 'translateZ' , 'rotateX' , 'rotateY' , 'rotateZ', 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility')
        mc.parent(switchCmdGrp , footCmd_bridge)
        mc.connectAttr(footCmd_bridge+'.ikfk' , switchCmdGrp+'.ikfkswitch')


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#

        #clean system
        mc.setAttr('footspace_for_rig_grp.visibility' , 0)
        footGrp = mc.createNode('transform' , n = dir+'_footRig_grp')
        commandGrp = [footCmd_bridge , toeroll_bridge , ballroll_bridge]
        pointjoint = [dir+'_Heel_Drv' , dir+'_Inside_Drv' , dir+'_Outside_Drv']
        mc.parent(commandGrp , footGrp)
        mc.parent(ik01sys[0] , toe_loc)
        mc.parent(ik02sys[0] , toeroll_loc)
        mc.parent(foot_loc+'_pos' , footGrp)
        mc.select(foot_loc)
        rc.makeObjZero('group' , 'zero' , 'On')
        mc.setAttr(foot_loc+'_zero.visibility' , 0)
        rc.lockhideAttr(toeroll_bridge , 0,'translateX' , 'translateY' , 'translateZ' , 'rotateX' , 'rotateY' , 'rotateZ', 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility')
        rc.lockhideAttr(ballroll_bridge , 0,'translateX' , 'translateY' , 'translateZ' , 'rotateX' , 'rotateY' , 'rotateZ', 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility')
        mc.delete(pointjoint)

        for x in loclist:
                rc.lockhideAttr(x,0,'translateX' , 'translateY' , 'translateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility')
        mc.select(cl=1)


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#

        #create curve control
        #fkctrl
        for x in fkJnt:
                mc.select(x)
                rc.makeObjZero('group' , 'zero' , 'On')
                rc.lockhideAttr(x , 0, 'translateX' , 'translateY' , 'translateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility','radius')

        if  mc.objExists('Space_Ctr'):
                crvscale = mc.getAttr('foot_Space_Ctr.otherscale')
        else:
                crvscale = mc.getAttr('foot_Space_Ctr.SCALE')

        ra.addShape( fkJnt[1], 'D07_circle' , 17 )
        mc.select( fkJnt[1]+'Shape.cv[0:7]' )
        mc.scale(5*crvscale , 5*crvscale , 5*crvscale)
        mc.select(cl=True)

#rigfoot('Left')
#first pass 2012/11/26